﻿using KinonekoSoftware.Foundation.Languages;
using KinonekoSoftware.Foundation.UI.Commands;
using KinonekoSoftware.Foundation;

namespace KinonekoSoftware.Foundation.UI.ViewModels
{
    public sealed class InternalErrorDialogVM : DialogBase
    {
        private DialogCloseHandler _handler;
        public InternalErrorDialogVM(){}
        
        public InternalErrorDialogVM(Exception exception, Type type)
        {
            Exception      = exception;
            ErrorViewModel = type;
            CancelCommand  = new ActionCommand(Cancel);
        }
        
        internal override void OnReceiveInternal(DialogContext dc)
        {
            _handler         = dc.CloseHandler;
            CancelButtonText = string.IsNullOrEmpty(CancelButtonText) ? LanguageManager.CancelButtonText : CancelButtonText;
        }
        
        /// <summary>
        /// 
        /// </summary>
        public void Cancel()
        {
            //
            // 关闭对话框
            _handler(this);
        }
        
        public Exception Exception { get; }
        public Type ErrorViewModel { get; }

        public string ExceptionText
        {
            get
            {
                return $"错误消息：\n{Exception.Message}\n\n视图模型：\n{ErrorViewModel.FullName}";
            }
        }

        protected override bool CanFinish() => true;
    }

    public sealed class InternalErrorPageVM : ViewModelBase
    {
        public InternalErrorPageVM(){}
        
        public InternalErrorPageVM(Exception exception)
        {
            Exception      = exception;
            ErrorViewModel = "Null";
        }
        
        public InternalErrorPageVM(Exception exception, Type type)
        {
            Exception      = exception;
            ErrorViewModel = type.FullName;
        }
        
        public Exception Exception { get; }
        public string ErrorViewModel { get; }
        public string ExceptionText
        {
            get
            {
                return $"错误消息：\n{Exception.Message}\n\n视图模型：\n{ErrorViewModel}";
            }
        }
    }
}